package cn.dam.controller;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.dam.base.ResponseBase;
import cn.dam.constants.Constants;
import cn.dam.feign.MemberServiceFeign;
import cn.dam.utils.CookieUtil;
@Controller
public class IndexController {
	private static final String INDEX="index";
	@Autowired
	private MemberServiceFeign memberServiceFeign;
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String index(HttpServletRequest request) {
		//从cookie中获取token信息
		String token=CookieUtil.getUid(request,Constants.COOKIE_MEMBER_TOKEN);
		//如果cookie中存在token，则查询会员服务接口，使用token查询用户信息
		if(StringUtils.isNotEmpty(token)) {
			ResponseBase responseBase = memberServiceFeign.findByTokenUser(token);
			if(responseBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
				LinkedHashMap userData= (LinkedHashMap) responseBase.getData();
				String username = (String) userData.get("username");
				request.setAttribute("username",username);
				return INDEX;
			}
			
		}
		return "redirect:login";
		
	}
}
